from config import *


class NativeBayes:
    def __init__(self):
        self._x = self._y = None
        self._data = self._func = None
        self._n_possibilities = None
        self._labelled_x = self._label_zip = None
        self._cat_counter = self._con_counter = None
        self._label_dic = self._feat_dics = None

    def __getitem__(self, item):
        if isinstance(item, str):
            return getattr(self, "_" + item)

    def feed_data(self, x, y, sample_weight=None):
        pass

    def feed_sample_weight(self, sample_weight=None):
        pass

    def get_prior_probability(self, lb=1):
        return [float(_c_num + lb) / float(len(self._y) + float(lb * len(self._cat_counter))) for _c_num in self._cat_counter]

    def fit(self, x=None, y=None, sample_weight=None, lb=1):
        if x is not None and y is not None:
            self.feed_data(x, y, sample_weight)
        self._func = self._fit(lb)

    def _fit(self, lb):
        pass

    def predict_one(self, x, get_raw_result=False):
        if type(x) is np.ndarray:
            x = x.tolist()
        else:
            x = x[:]
        x = self._transfer_x(x)
        m_arg, m_probability = 0, 0
        for i in range(len(self._cat_counter)):
            p = self._func(x, i)
            if p > m_probability:
                m_arg, m_probability = i, p
        if not get_raw_result:
            return self.label_dic[m_arg]
        return m_probability

    def predict(self, x, get_raw_result=False, **kwargs):
        return np.array([self.predict_one(xx, get_raw_result) for xx in x])

    def _transfer_x(self, x):
        return x

    def evaluate(self,x,y):
        y_pred = self.predict(x)
        print ("Acc: {:12.6} %".format(100*float(np.sum(y_pred == y))/len(y)))


































